package com.car.carsharing.service.impl;

import com.car.carsharing.mapper.CarMapper;
import com.car.carsharing.mapper.PayMapper;
import com.car.carsharing.mapper.UserCouponMapper;
import com.car.carsharing.pojo.Car;
import com.car.carsharing.pojo.Pay;
import com.car.carsharing.pojo.UserCoupon;
import com.car.carsharing.service.PayService;
import com.car.carsharing.util.StringUtils;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PayServiceImpl implements PayService {
    @Autowired
    PayMapper payMapper;
    @Autowired
    CarMapper carMapper;
    @Autowired
    UserCouponMapper userCouponMapper;
    @Override
    public int driving(Map map) {
        Pay p = new Pay();
        p.setUserId((Integer) map.get("userId"));
        p.setCarId((Integer) map.get("carId"));

        //判断是否已有使用的车辆
        Car c = payMapper.selectUseCar(p);
        if(c !=null){
            return -1;
        }

        p.setStartTime(StringUtils.getTime(new Date()));

        Car car = new Car();
        car.setStatus("2");
        car.setId((Integer) map.get("carId"));
        carMapper.updateCar(car);

        return payMapper.insertDriving(p);
    }

    @Override
    public Car selectUseCar(Map map) {
        Pay p = new Pay();
        p.setUserId((Integer) map.get("userId"));
        p.setCarId((Integer) map.get("carId"));

        //判断是否已有使用的车辆
        Car c = payMapper.selectUseCar(p);
       return c;
    }

    @Override
    public Pay updatePay(Map map) {
       Integer userId = (Integer) map.get("userId");
       Integer carId =  (Integer) map.get("carId");

        Pay pay = payMapper.selectPayByUserIdWithCarId(userId,carId);

        if(pay.getEndTime() == null || "".equals(pay.getEndTime())){
            pay.setEndTime(StringUtils.getTime(new Date()));
        }

        //立即支付
        if("1".equals(map.get("curFlag"))){
            if(map.get("couponId") != null && !"".equals(map.get("couponId") )){
                pay.setCouponId((Integer) map.get("couponId"));
            }
            pay.setPayFlag((String) map.get("payFlag"));
            if("1".equals(map.get("payFlag"))){
                pay.setPayTime(StringUtils.getTime(new Date()));
            }

            Long time = StringUtils.effectiveTime(pay.getEndTime(), pay.getStartTime());
            pay.setMoney((0.5*(time/1000/60))+"");
            //支付金额
            if(map.get("couponId") != null && !"".equals(map.get("couponId") )){
                pay.setMoney(0+"");
            }

            //修改优惠卷
            UserCoupon uc = new UserCoupon();
            uc.setUserId(userId);
            uc.setCouponId( (map.get("couponId") != null && !"".equals(map.get("couponId") )) ? (Integer)map.get("couponId"):-1);


           UserCoupon userCoupon = userCouponMapper.selectUserCoupon(uc);

           if(userCoupon != null){
               if(!StringUtils.isDateTime(userCoupon.getEffectiveTime())){
                   Integer count = Integer.parseInt(userCoupon.getEffectiveTime());
                   if(count == 0){
                       count = 0;
                   }else{
                       count -=1;
                   }
                   userCoupon.setEffectiveTime(count+"");
                   userCouponMapper.updateUserCoupon(userCoupon);
               }
           }


        }

        payMapper.updatePay(pay);

        Car car = new Car();
        car.setId(carId);
        car.setStatus("0");

        carMapper.updateCar(car);

        return payMapper.selectPayByUserIdWithCarId(userId,carId);
    }

    @Override
    public Pay selectPay(Map map) {
        Integer userId = (Integer) map.get("userId");

        return payMapper.selectPayByUserId(userId);
    }

    @Override
    public List<Pay> getMyPay(Integer userId) {
        return payMapper.selectPayedByUserId(userId);
    }
}
